<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel='$emit("close")'
      @confirm='onConfirm'
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateBirthday',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(this.value)
    }
  },
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true
      })
      const data = dayjs(this.currentDate).format('YYYY-MM-DD')
      await updateUserProfile({
        birthday: data
      })
      this.$toast.success('保存成功')
      this.$emit('close')
      // 修改成功更新视图的方式
      //  1. 重新获取数据
      //  2. 单独前端更新字段(推荐)
      this.$emit('input', data)// 方式1 可以使用v-model
    }
  }
}

</script>
<style lang='less' scoped>
</style>
